package com.ruoyi.report_wjjhl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.ruoyi.common.utils.BigDecimalUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.syrw.annotation.FIllBasicInfo;
import com.ruoyi.syrw.common.LineFunction;
import com.ruoyi.syrw.common.LineUtil;
import com.ruoyi.report_tg.domain.vo.GeneralCharDataVo;
import com.ruoyi.report_wjjhl.domain.WdcledtaddbzqxBaDetail;
import com.ruoyi.report_wjjhl.domain.vo.WdcledtaddbzqxBaDetailVo;
import com.ruoyi.report_wjjhl.mapper.WdcledtaddbzqxBaDetailMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.report_wjjhl.domain.bo.WdcledtaddbzqxBsInfoBo;
import com.ruoyi.report_wjjhl.domain.vo.WdcledtaddbzqxBsInfoVo;
import com.ruoyi.report_wjjhl.domain.WdcledtaddbzqxBsInfo;
import com.ruoyi.report_wjjhl.mapper.WdcledtaddbzqxBsInfoMapper;
import com.ruoyi.report_wjjhl.service.IWdcledtaddbzqxBsInfoService;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 无机结合料稳定材料EDTA滴定标准曲线试验检测记录Service业务层处理
 *
 * @author zyb
 * @date 2024-08-05
 */
@RequiredArgsConstructor
@Service
@DSTransactional
public class WdcledtaddbzqxBsInfoServiceImpl implements IWdcledtaddbzqxBsInfoService {

    private final WdcledtaddbzqxBsInfoMapper baseMapper;
    private final WdcledtaddbzqxBaDetailMapper detailMapper;

    /**
     * 查询无机结合料稳定材料EDTA滴定标准曲线试验检测记录
     */
    @Override
    @FIllBasicInfo
    public WdcledtaddbzqxBsInfoVo queryById(Long excelId){
        WdcledtaddbzqxBsInfoVo wdcledtaddbzqxBsInfoVo = baseMapper.selectVoById(excelId);
        // 查询detail
        List<WdcledtaddbzqxBaDetailVo> baDetailVos = detailMapper.selectVoList(new LambdaQueryWrapper<WdcledtaddbzqxBaDetail>()
            .eq(WdcledtaddbzqxBaDetail::getExcelId, excelId));
        if (CollUtil.isNotEmpty(baDetailVos)) {
            wdcledtaddbzqxBsInfoVo.setBaDetailVos(baDetailVos);
        }
        return compute(wdcledtaddbzqxBsInfoVo);
    }

    /**
     * 查询无机结合料稳定材料EDTA滴定标准曲线试验检测记录列表
     */
    @Override
    public TableDataInfo<WdcledtaddbzqxBsInfoVo> queryPageList(WdcledtaddbzqxBsInfoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<WdcledtaddbzqxBsInfo> lqw = buildQueryWrapper(bo);
        Page<WdcledtaddbzqxBsInfoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询无机结合料稳定材料EDTA滴定标准曲线试验检测记录列表
     */
    @Override
    public List<WdcledtaddbzqxBsInfoVo> queryList(WdcledtaddbzqxBsInfoBo bo) {
        LambdaQueryWrapper<WdcledtaddbzqxBsInfo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<WdcledtaddbzqxBsInfo> buildQueryWrapper(WdcledtaddbzqxBsInfoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<WdcledtaddbzqxBsInfo> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getTotalPage() != null, WdcledtaddbzqxBsInfo::getTotalPage, bo.getTotalPage());
        lqw.eq(bo.getPage() != null, WdcledtaddbzqxBsInfo::getPage, bo.getPage());
        lqw.eq(StringUtils.isNotBlank(bo.getJilbh()), WdcledtaddbzqxBsInfo::getJilbh, bo.getJilbh());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancdwmc()), WdcledtaddbzqxBsInfo::getJiancdwmc, bo.getJiancdwmc());
        lqw.eq(StringUtils.isNotBlank(bo.getGongcmc()), WdcledtaddbzqxBsInfo::getGongcmc, bo.getGongcmc());
        lqw.eq(StringUtils.isNotBlank(bo.getGongcbwyt()), WdcledtaddbzqxBsInfo::getGongcbwyt, bo.getGongcbwyt());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpmc()), WdcledtaddbzqxBsInfo::getYangpmc, bo.getYangpmc());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpbh()), WdcledtaddbzqxBsInfo::getYangpbh, bo.getYangpbh());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpsl()), WdcledtaddbzqxBsInfo::getYangpsl, bo.getYangpsl());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpzt()), WdcledtaddbzqxBsInfo::getYangpzt, bo.getYangpzt());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpsj()), WdcledtaddbzqxBsInfo::getYangpsj, bo.getYangpsj());
        lqw.eq(StringUtils.isNotBlank(bo.getShiyjcrq()), WdcledtaddbzqxBsInfo::getShiyjcrq, bo.getShiyjcrq());
        lqw.eq(StringUtils.isNotBlank(bo.getShiytjwd()), WdcledtaddbzqxBsInfo::getShiytjwd, bo.getShiytjwd());
        lqw.eq(StringUtils.isNotBlank(bo.getShiytjsd()), WdcledtaddbzqxBsInfo::getShiytjsd, bo.getShiytjsd());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancyj()), WdcledtaddbzqxBsInfo::getJiancyj, bo.getJiancyj());
        lqw.eq(StringUtils.isNotBlank(bo.getPandyj()), WdcledtaddbzqxBsInfo::getPandyj, bo.getPandyj());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancyjIds()), WdcledtaddbzqxBsInfo::getJiancyjIds, bo.getJiancyjIds());
        lqw.eq(StringUtils.isNotBlank(bo.getPandyjIds()), WdcledtaddbzqxBsInfo::getPandyjIds, bo.getPandyjIds());
        lqw.eq(StringUtils.isNotBlank(bo.getZhuyyqsbmcjbh()), WdcledtaddbzqxBsInfo::getZhuyyqsbmcjbh, bo.getZhuyyqsbmcjbh());
        lqw.eq(StringUtils.isNotBlank(bo.getShebIds()), WdcledtaddbzqxBsInfo::getShebIds, bo.getShebIds());
        lqw.eq(StringUtils.isNotBlank(bo.getFujsm()), WdcledtaddbzqxBsInfo::getFujsm, bo.getFujsm());
        lqw.eq(StringUtils.isNotBlank(bo.getFangcs()), WdcledtaddbzqxBsInfo::getFangcs, bo.getFangcs());
        lqw.eq(bo.getCsk() != null, WdcledtaddbzqxBsInfo::getCsk, bo.getCsk());
        lqw.eq(bo.getCsb() != null, WdcledtaddbzqxBsInfo::getCsb, bo.getCsb());
        return lqw;
    }

    /**
     * 新增无机结合料稳定材料EDTA滴定标准曲线试验检测记录
     */
    @Override
    public Boolean insertByBo(WdcledtaddbzqxBsInfoBo bo) {
        WdcledtaddbzqxBsInfo add = BeanUtil.toBean(bo, WdcledtaddbzqxBsInfo.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setExcelId(add.getExcelId());
            // 新增详细 5次
            ArrayList<WdcledtaddbzqxBaDetail> details = new ArrayList<>(5);
            for (long i = 1; i <= 5L; i++) {
                details.add(new WdcledtaddbzqxBaDetail(add.getExcelId()));
            }
            detailMapper.insertBatch(details);
        }
        return flag;
    }

    /**
     * 修改无机结合料稳定材料EDTA滴定标准曲线试验检测记录
     */
    @Override
    public Boolean updateByBo(WdcledtaddbzqxBsInfoBo bo) {
        WdcledtaddbzqxBsInfo update = BeanUtil.toBean(bo, WdcledtaddbzqxBsInfo.class);
        validEntityBeforeSave(update);
        boolean flag = baseMapper.updateById(update) > 0;
        if (flag) {
            detailMapper.updateBatchById(BeanUtil.copyToList(bo.getBaDetailVos(), WdcledtaddbzqxBaDetail.class));
        }
        return flag;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(WdcledtaddbzqxBsInfo entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除无机结合料稳定材料EDTA滴定标准曲线试验检测记录
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    /**
     * 计算 无机结合料稳定材料EDTA滴定标准曲线试验检测记录
     */
    @Override
    public WdcledtaddbzqxBsInfoVo compute(WdcledtaddbzqxBsInfoVo vo) {
        // 1. 参数判断 如果有任何一个为空,则 直接原样返回
        List<WdcledtaddbzqxBaDetailVo> detailVos = vo.getBaDetailVos();
        if (CollUtil.isEmpty(detailVos)) {
            return vo;
        }

        // 1.2 非空判断
        for (WdcledtaddbzqxBaDetailVo detailVo : detailVos) {
            if (BigDecimalUtil.isAnyOneEmpty(detailVo.getEdtahlpjz(), detailVo.getHuijl())) {
                return vo;
            }
        }
        double[][] data = new double[detailVos.size()][2];
        for (int i = 0; i < detailVos.size(); i++) {
            data[i][0] = detailVos.get(i).getHuijl().doubleValue();
            data[i][1] = detailVos.get(i).getEdtahlpjz().doubleValue();
        }
        // 2 计算
        LineFunction generalLine = LineUtil.getGeneralLine(data);
        // 3 画图的数据
        // 3.1 画图所需要的 data  参与画线 不展示 不突出辅助线
        List<GeneralCharDataVo> charData = generateData(generalLine, detailVos.get(0).getHuijl().doubleValue(),
            detailVos.get(detailVos.size()-1).getHuijl().doubleValue());
        // 3.2 其他的点 不参与画线 展示 不突出辅助线
        for (double[] datum : data) {
            charData.add(new GeneralCharDataVo(datum, "010"));
        }
//        // 3.3 参与画线 展示 突出辅助线
//        charData.add(new GeneralCharDataVo(new double[]{vo.getZuijhsl().doubleValue(), vo.getZuidgmd().doubleValue()}, "011"));
        vo.setChartData(charData);
        // 4 函数式
        vo.setFangcs("y=" + BigDecimalUtil.defaultValue(generalLine.getK(), 3)
            + "x+" + BigDecimalUtil.defaultValue(generalLine.getB(), 3));

        return vo;
    }

    private List<GeneralCharDataVo> generateData(LineFunction generalLine, double start, double end) {
        List<GeneralCharDataVo> result = new ArrayList<>(20);
        result.add(new GeneralCharDataVo(new double[]{BigDecimalUtil.defaultValue(start,2).doubleValue(), generalLine.getY(start)}, "100"));
        result.add(new GeneralCharDataVo(new double[]{BigDecimalUtil.defaultValue(end,2).doubleValue(), generalLine.getY(end)}, "100"));
        return result;
    }

}
